package com.example.protocol.paxos;

public class Proposer {
    private int proposalId;
    private String value;
    private Acceptor[] acceptors;

    public Proposer(int proposalId, String value, Acceptor[] acceptors) {
        this.proposalId = proposalId;
        this.value = value;
        this.acceptors = acceptors;
    }

    public boolean propose() {
        int promises = 0;
        for (Acceptor acceptor : acceptors) {
            if (acceptor.prepare(proposalId)) {
                promises++;
            }
        }

        if (promises > acceptors.length / 2) {
            int accepts = 0;
            for (Acceptor acceptor : acceptors) {
                if (acceptor.accept(proposalId, value)) {
                    accepts++;
                }
            }

            return accepts > acceptors.length / 2;
        }

        return false;
    }
}

